<?php
/**
 * 供应商付款方式管理
 * Created by PhpStorm.
 * User: z
 * Date: 14-8-30
 * Time: 下午3:29
 */
class Controller_Payway extends Controller_Admin
{
    public function before()
    {
        try{
            ACL::Required('supplier payways view');
        }catch(HTTP_Exception_403 $e){
            Message::error('sorry，你没有权限进这个目录');
            $this->request->redirect(URL::base(TRUE));
        }

        parent::before();
    }
    /**
     * 查看全部的付款方式
     *
     */
    public function action_index()
    {
         $this->title = __("付款方式管理");
         $view = View::factory('payway/index');

         try{
             $condtion = array();
             if($this->request->is_post()
                and (isset($_POST['status']))
                and ($_POST['status'] != 'all'))
             {
                 $condtion['status'] = $_POST['status'];
             }
             $payway_lists = Payway::get_payway_lists($condtion);//var_dump($payway_lists);
         }
         catch(Exception $e)
         {
             $payway_lists = NULL;
             Message::error('系统出错,请联系IT！');
         }


         $view->set('payway_lists',$payway_lists);
         $view->set('status_select',isset($condtion['status'])?$condtion['status']:NULL);
         $this->response->body($view);
    }

    /**
     * 更改付款方式的激活状态
     * id
     * status_next
     */
    public function action_changestatus()
    {
        $re = array('code'=>2,'msg'=>'操作失败');
        if(isset($_POST['id']) and isset($_POST['status_next']))
        {
            try{
                $condtion['id'] = trim($_POST['id']);
                $condtion['status'] = trim($_POST['status_next']);
                $rows = Payway::do_update($condtion);

                if($rows > 0)
                {
                    $re['code'] = 1;
                    $re['msg'] = "操作成功";
                }
            }catch (Exception $e)
            {
                $re['code'] = 9;
                $re['msg'] = "系统错误";
            }
        }
        echo json_encode($re);exit;
    }

    /**
     * 更改付款方式的名称
     * id
     * paywayname
     */
    public function action_changename()
    {
        $re = array('code'=>2,'msg'=>'操作失败');

        if(isset($_POST['id'])
            and isset($_POST['paywayname']))
        {
            try{
                $payways = array();
                $payways_id = trim($_POST['id']);
                $payways['paywayname'] = trim($_POST['paywayname']);

                $payways_old = DB::select('id','paywayname')
                    ->from('payways')
                    ->where('id','=',$payways_id)
                    ->execute('newerp')
                    ->current();

                if($payways_old
                    and isset($payways_old['paywayname'])
                    and ($payways_old['paywayname'] != $payways['paywayname']))
                {
                    $payways['updatetime'] = date('Y-m-d H:i:s');
                    $update_ok = DB::update('payways')
                        ->set($payways)
                        ->where('id','=',$payways_id);
                    $update_ok = $update_ok->execute('newerp');

                    if($update_ok > 0)
                    {
                        $re['code'] = 1;
                        $re['msg'] = "操作成功";
                    }
                }
            }catch (Database_Exception $e)
            {
                $re['code'] = 9;
                $re['msg'] = "系统错误";
            }
        }
        echo json_encode($re);exit;
    }

    /**
     * 处理添加的付款方式
     * 添加成功提示成功
     */
    public function action_add()
    {
        if(isset($_POST['paywayname']) and trim($_POST['paywayname']))
        {
            $old_data = array();
            $old_data['paywayname'] = trim($_POST['paywayname']);

            /**
             * 捕获数据库错误信息
             */
            try{
                $re = Payway::insert_payway($old_data);
            }
            catch(Exception $e)
            {
                Message::error('系统出错,请联系IT！');
            }


            if($re)
            {
                Message::success('添加成功');
            }
            else
            {
                Message::error('添加失败');
            }

        }

        $this->request->redirect('supplier/payway/index');
    }

}